package com.idak.rdv.web.controller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.idak.rdv.web.form.LoginForm;
import com.idak.rdv.web.validator.LoginFormValidator;

@Controller
@RequestMapping("/login")
public class AuthentificationController {
	
	@Autowired
	private LoginFormValidator loginFormValidator = null;
	
	@RequestMapping(method=RequestMethod.GET)
	public String initAutentificationForm(Map<String, Object> model){
		LoginForm form = new LoginForm();
		model.put("loginForm", form);
		return "loginForm";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String  submitAuthentificationForm(@ModelAttribute("loginForm") @Valid LoginForm loginForm, BindingResult result){
		String forward = "welcome";
		loginFormValidator.validate(loginForm, result);
		if(result.hasErrors()){
			 forward = "loginForm";
		}else{
			forward = "welcome";
		}
		return forward;
	}
}
